﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Timetable.Site.Models;
using Timetable.Site.Models.Schedule;
using Schedule = Timetable.Site.Host.Schedule.Schedule;

namespace Timetable.Site.Controllers.Api
{
    public class ScheduleController : BaseApiController<Schedule>
    {
        /// <summary>
        /// Возвращает расписание для выбранного преподавателя
        /// </summary>
        /// <param name="lecturerId">Идентификатор преподавателя</param>
        /// <returns></returns>
        public HttpResponseMessage GetForLecturer(int lecturerId)
        {
            return CreateResponse<int, IEnumerable<Schedule>>(privateGetForLecturer, lecturerId);
        }


        private IEnumerable<Schedule> privateGetForLecturer(int lecturerId)
        {
            var result = ScheduleService.GetAllForLecturer(lecturerId);

            return result;
        }

        /// <summary>
        /// Возвращает расписание для выбранной специальности
        /// </summary>
        /// <param name="facultyId">Идентификатор факультета</param>
        /// <param name="courseId">Идентификатор курса</param>
        /// <param name="specialityId">Идентификатор специальности</param>
        /// <returns></returns>
        public HttpResponseMessage GetForSpeciality(int facultyId, int courseId, int specialityId)
        {
            return CreateResponse<int, int, int, IEnumerable<Schedule>>(privateGetForSpeciality, facultyId, courseId, specialityId);
        }


        private IEnumerable<Schedule> privateGetForSpeciality(int facultyId, int courseId, int specialityId)
        {
            var result = ScheduleService.GetAllForSpeciality(facultyId, courseId, specialityId);

            return result;
        }

        /// <summary>
        /// Возвращает расписание для выбранного курса
        /// </summary>
        /// <param name="facultyId">Идентификатор факультета</param>
        /// <param name="courseId">Идентификатор курса</param>
        /// <returns></returns>
        public HttpResponseMessage GetForCourse(int facultyId, int courseId)
        {
            return CreateResponse<int, int, IEnumerable<Schedule>>(privateGetForCourse, facultyId, courseId);
        }


        private IEnumerable<Schedule> privateGetForCourse(int facultyId, int courseId)
        {
            var result = ScheduleService.GetAllForCourse(facultyId, courseId);

            return result;
        }


        /// <summary>
        /// Возвращает расписание для выбранной группы
        /// </summary>
        /// <param name="facultyId">Идентификатор факультета</param>
        /// <param name="courseId">Идентификатор курса</param>
        /// <param name="groupId">Идентификатор группы</param>
        /// <returns></returns>
        public HttpResponseMessage GetForGroup(int facultyId, int courseId, int groupId)
        {
           return CreateResponse<int, int, int, IEnumerable<Schedule>>(privateGetForGroup, facultyId, courseId, groupId);
        }


        private IEnumerable<Schedule> privateGetForGroup(int facultyId, int courseId, int groupId)
        {
            var result = ScheduleService.GetAllForGroup(facultyId, courseId, groupId);

            return result;
        }

        /// <summary>
        /// Возвращает расписание для выбранной аудитории
        /// </summary>
        /// <param name="auditoriumId">Идетификатор аудитории</param>
        /// <returns></returns>
        public HttpResponseMessage GetForAuditorium(int auditoriumId)
        {
            return CreateResponse<int, IEnumerable<Schedule>>(privateGetForAuditorium, auditoriumId);
        }

        private IEnumerable<Schedule> privateGetForAuditorium(int auditoriumId)
        {
            return ScheduleService.GetAllForAuditorium(auditoriumId);
        }

        /// <summary>
        /// Возвращает расписание по идентификатору
        /// </summary>
        /// <param name="id">Идетификатор расписания</param>
        /// <returns></returns>
        public HttpResponseMessage GetById(int id)
        {
            return CreateResponse<int, Schedule>(privateGetById, id);
        }

        
        private Schedule privateGetById(int id)
        {
            return ScheduleService.GetById(id);
        }

        /// <summary>
        /// Добавить новую запись в расписание
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage Add(AddModel model)
        {
            return CreateResponse(privateAdd, model);
        }
        private void privateAdd(AddModel model)
        {
           // var auditorium = Service.GetAuditoriumByNumber(model.Auditorium, 1);
            var lecturer = LecturerService.GetByFirstMiddleLastname(model.Lecturer);  
            var group = GroupService.GetGroupById(model.Group.Id);
            var tutorial = TutorialService.GetTutorialById(model.Tutorial.Id);
            var tutorialType = TutorialTypeService.GetTutorialTypeById(model.TutorialType.Id);

            /*
            foreach (var time in model.Times)
            {
                var period = DataService.GetPeriod(time.Id);

                foreach (var dayOfWeek in model.DaysOfWeek)
                {
                    var schedule = new Schedule
                    {
                        //Auditorium = auditorium,
                        Period = period,
                        DayOfWeek = dayOfWeek
                    };
                    DataService.Add(schedule);
                }
            }*/
        }

        /// <summary>
        /// Обновить выбранную запись в расписании
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public HttpResponseMessage Update(UpdateModel model)
        {
            return CreateResponse(privateUpdate, model);
        }
        private void privateUpdate(UpdateModel model)
        {
            //var auditorium = DataService.GetAuditoriumByNumber(model.Auditorium, 1);
            //var lecturer = DataService.GetLecturerByFirstMiddleLastname(model.Lecturer);
            //var group = DataService.GetGroup(model.Group.Id);
            //var tutorial = DataService.GetTutorial(model.Tutorial.Id);
            //var tutorialType = DataService.GetTutorialType(model.TutorialType.Id);
            //var period = DataService.GetPeriod(model.Time.Id);

            //var schedule = DataService.GetScheduleById(model.Id);
            //schedule.Auditorium = auditorium;
            //schedule.Period = period;
            //schedule.DayOfWeek = model.DaysOfWeek;

            //DataService.UpdateSchedule(schedule);
            throw new NotImplementedException();
        }

        /// <summary>
        /// Удалить запись из расписания
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        
        [HttpPost]
        public HttpResponseMessage Delete(DeleteModel model)
        {
            return CreateResponse(privateDelete, model.Id);
        }

       
        private void privateDelete(int id)
        {
            var schedule = ScheduleService.GetById(id);
            //DataService.Delete(schedule);
        }
    }
}